package com.baidu.ocrgeneralocrdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TestPhotoImageActivity extends AppCompatActivity {
    private static final String TAG = "PhotoImageTestActivity";
    public static final int RC_CHOOSE_PHOTO = 2;
    private ImageView photoImgView = null;
    private TextView photoPredictResultView = null;
    private Activity activity = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_photo_image);

        activity = this;
        photoImgView = findViewById(R.id.photo_iamge);
        photoPredictResultView = findViewById(R.id.photo_predict_result);

        Button btChosePic = findViewById(R.id.bt_chose_pic);
        btChosePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
                intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intentToPickPic, RC_CHOOSE_PHOTO);
            }
        });
    }

    // 选取图片后进行图片预测
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case RC_CHOOSE_PHOTO:
                if (resultCode == RESULT_OK){
                    Uri imageUri = data.getData();
                    if (imageUri != null) {
                        InputStream input = null;
                        try {
                            input = getContentResolver().openInputStream(imageUri);
                            Bitmap bitmap = BitmapFactory.decodeStream(input);
                            PredictorWrapper.asyncTestOneImage(activity, bitmap, photoImgView, photoPredictResultView, new PredictorResultListener() {
                                @Override
                                public void ResultListener(String result) throws IOException {

                                }
                            });
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (input != null) {
                                    input.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                break;
        }
    }
}
